﻿#include <iostream>
#include <locale>

#include <cmath>

#include <boost/bind.hpp>

#include <boost/thread.hpp>

#include "webui.h"
#pragma comment(lib,"webui.lib")

struct Point
{
	webui::JSON rotate(const webui::Query& GET)
	{
		int dphi = GET["dphi"];
		double R = sqrt(x*x+y*y);
		double phi = atan(y/x)+dphi*3.14/180;

		webui::JSON r;
		r["x"] = x= R*cos(phi);
		r["y"] = y= R*sin(phi);
		return r;
	}

	Point() : x(0),y(0) {}
	double x,y;
};

webui::JSON move(Point& pt,const webui::Query& GET)
{
	return webui::JSON
		( "x", pt.x+=GET.cast<int>("dx") )
		( "y", pt.y+=GET.cast<int>("dy") ) 
		;
}

int main()
{
	setlocale(LC_ALL,"Russian");

	try
	{
		Point pt;

		webui::callback_map_t cb_map;
		cb_map["move"]=boost::bind( move,boost::ref(pt),_1 );
		cb_map["rotate"]=boost::bind( &Point::rotate,&pt,_1 );

		//webui::Server srv(cb_map);
		//srv.run(1337);
		
		boost::thread( &webui::Server::Run, webui::Server(cb_map)/*this*/, 1337/*port*/ );

		for(char answ=0;answ!='Q';)
		{
			std::cout<<"type Q for Quit: ";
			std::cin>>answ;
		}
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
		return 0xBAD;
	}
	return 0;
}